package com.base.test.mutual.utils;

import com.alibaba.fastjson.JSON;
import com.base.commons.tools.exception.BaseOfException;
import com.base.commons.tools.utils.CameTools;
import com.base.commons.tools.utils.HttpConnectUtil;
import com.base.commons.tools.utils.MD5;
import com.base.test.mutual.dto.FmsReqData;
import com.base.test.mutual.exception.ModuleErrorCode;
import com.base.test.mutual.properties.FmsProperties;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author carl
 * @since 2020/1/19
 */
@Log4j2
@Configuration
@EnableConfigurationProperties(FmsProperties.class)
public class FMSHttpUtils {

    @Autowired
    private FmsProperties fmsProperties;

    @Autowired
    private HttpServletRequest httpServletRequest;
    /**
     * fms 远程接口
     * @param requestObj 请求参数
     * @param url 接口路径
     * @param method 方法
     * @param clazz 响应结果的类型
     * @param <T> 响应结果的类型
     * @return
     */
    public  <T> T execute(T requestObj, String url, String method, Class<?> clazz)  {
        String request= null;
        Object obj=null;
        ResponseEntity res=null;
        FmsReqData requestParam= new FmsReqData();
        try {
            requestParam.setRequestId(UUID.randomUUID().toString());
            requestParam.setRequestTime(String.valueOf(System.currentTimeMillis()));
            requestParam.setAppCode(fmsProperties.getAppCode());
            requestParam.setSign(MD5.MD5Encode(requestParam.getRequestId()+
                    requestParam.getRequestTime()+fmsProperties.getEncryKey()));
            requestParam.setRequestData(JSON.parseObject(CameTools.toUnderlineJSONStringIncludeNull(requestObj)));
            //requestParam.setRequestData(JSONObject.toJSON(requestObj));
            //request = JSONObject.toJSON(requestParam).toString();
            request = CameTools.toUnderlineJSONStringIncludeNull(requestParam);
            url=fmsProperties.getUrl()+url;
            HttpHeaders headers = new HttpHeaders();
            String language = httpServletRequest.getHeader(HttpHeaders.ACCEPT_LANGUAGE);
            headers.add(HttpHeaders.ACCEPT_LANGUAGE,language);
            log.info("模块Module:{},url:{},请求参数[{}]","FMS",url,request);
            res = HttpConnectUtil.tempHttp(request, url, headers, method, new HashMap<>(), clazz,fmsProperties.getName(),fmsProperties.getPassword());
            obj = JSON.toJSONString(res.getBody());
            log.info("模块Module:{},url:{},请求结果：{}","FMS",url,obj);
        } catch (Exception e) {
            log.error("模块Module:{},url:{},请求参数[{}],异常信息：{}","FMS",url,request,e);
            throw new BaseOfException(ModuleErrorCode.FMS_FAILED,e);
        }
        return (T) obj;
    }

}